// pages/Case/addTrack/addTrack.js
import request from '../../../utils/config.js';
import util from '../../../utils/util.js';
const app = getApp();
let defaultSignDays = {
  "待补充材料后分配": 5,
  "案件沟通/补充材料中": 5,
  "待出分析报告": 3,
  "已出分析报告，建议通过，待评议": 2,
  "已出分析报告，建议放弃，待评议": 2,
  "已通过，待拟订合同": 3,
  "合同电子版已发": 5,
  "客户暂未签约，由客服跟进": 5,
  "合同条款沟通/修改中，待确认": 5,
  "合同条款已确认，纸质版已寄出": 5,
  "待定/暂缓跟踪": 20,
}
let diagnosisSignDays = {
  "待补充材料后分配": 5,
  "案件沟通/补充材料中": 5,
  "待出诊断报告": 3,
  "诊断报告已发送，开始定期案件跟踪": 10,
  "案件跟踪，向客户反馈案件相对方的情况": 10,
  "案件已回款，做结案处理": 20,
  "案件需要启动诉讼程序": 5,
  "待拟定委托代理合同": 3
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trackDesc:'',
    intervalDays: '',
    notifyUser: false,
    notifyInner: false,
    descList: [],
    intervalDaysObj:{},
    caseInfo:{},
    daysLarge: 5
  },
  change_trackDesc: function(e){
    console.log(e)
    this.setData({
      trackDesc: e.detail 
    })
  },
  change_intervalDays: function (e) {
    console.log(e)
    this.setData({
      intervalDays: e.detail.value
    })
  },
  change_notifyUser: function (e) {
    console.log(e)
    this.setData({
      notifyUser: e.detail.value
    })
  },
  change_notifyInner: function (e) {
    console.log(e)
    this.setData({
      notifyInner: e.detail.value
    })
  },
  descChoose:function(e){
    console.log(e)
    const index = e.detail.value
    this.setData({
      // trackDesc: util.timetrans(new Date(), 'day') + ' ' + this.data.descList[index],
      trackDesc:this.data.descList[index],
      intervalDays: this.data.intervalDaysObj[this.data.descList[index]],
      daysLarge: this.data.intervalDaysObj[this.data.descList[index]]
    })
  },
  cancle:function(){
    wx.navigateBack();
  },
  ok: function () {
    let { caseInfo, trackDesc, intervalDays, notifyUser, notifyInner}=this.data;
    let daysLarge = this.data.daysLarge;
    console.log('intervalDays李慧====',intervalDays);
    if (!trackDesc) {
      wx.showToast({
        title: '请输入描述',
        icon: 'none',
      })
      return;
    }
    if (!intervalDays) {
      wx.showToast({
        title: '请输入下次跟踪间隔天数',
        icon: 'none',
      })
      return;
    }
    if (isNaN(intervalDays)) {
      wx.showToast({
        title: '间隔天数格式错误',
        icon: 'none',
      })
      return;
    }
    if (caseInfo.caseBaseInfoVo.diagnosisSign == '1' && intervalDays > daysLarge) {
      wx.showToast({
        title: '跟踪间隔天数不得大于' + daysLarge,
        icon: 'none',
      })
      return;
    }
    if ( intervalDays < 1) {
      wx.showToast({
        title: '跟踪间隔天数不得小于1',
        icon: 'none',
      })
      return;
    }
    let send = {
      operId:app.globalData.operId,
      trackDesc: trackDesc,
      intervalDays :intervalDays,
      notifyUser: notifyUser ? 'Y' : 'N',
      notifyInner: notifyInner ? 'Y' : 'N',
      caseId: caseInfo.caseBaseInfoVo.caseId,
    }
    console.log(send)
    let _this = this;
    wx.showLoading({ title: ' ', mask: true })
    request.requestMisApi("post", "caseTracks", send, (res) => {
      console.log("添加", res)
      if (res.code == '00') {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        if (app.globalData.listRefresh.Case.tabtype =='newDynamic' ){
          app.globalData.listRefresh.Case = {
            refresh: true,
          }
        }
        setTimeout(function () {
          wx.navigateBack();
        }, 1000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (res) => {
      console.log("获取失败", res)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let intervalDaysObj = {}
    let descList = [];
    if (app.globalData.caseTrackInfo.caseBaseInfoVo.diagnosisSign == '2'){
      intervalDaysObj = diagnosisSignDays
      for (let key in diagnosisSignDays) {
        descList.push(key)
      }
    } else {
      for (let key in defaultSignDays) {
        descList.push(key)
      }
      intervalDaysObj=defaultSignDays
    }
    this.setData({ caseInfo: app.globalData.caseTrackInfo, descList,intervalDaysObj })
  },

  // 收藏取消
  collect: function () {
    if (this.data.isloading) {
      return
    }
    this.setData({
      isloading: true
    });
    let _this = this;
    if (this.data.caseInfo.caseBaseInfoVo.isFavor == "Y") {
      request.requestMisApi('get', "flyWorkPlace/unfavorCases/" + this.data.caseInfo.caseBaseInfoVo.caseId, {}, (res) => {
        console.log(res)
        if (res.code == '00') {
          let caseInfo = this.data.caseInfo;
          caseInfo.caseBaseInfoVo.isFavor = 'N';
          _this.setData({
            isloading: false, caseInfo
          });
        }
      }, (res) => {
        console.log("接口错误=", res)
      });
    } else {
      request.requestMisApi('get', "flyWorkPlace/favorCases/" + this.data.caseInfo.caseBaseInfoVo.caseId, {}, (res) => {
        console.log(res)
        if (res.code == '00') {
          let caseInfo = this.data.caseInfo;
          caseInfo.caseBaseInfoVo.isFavor = 'Y';
          _this.setData({
            isloading: false, caseInfo
          });
        }
      }, (res) => {
        console.log("接口错误=", res)
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})